// 1.导入相关依赖包
const express = require('express')
const { ApolloServer, gql } = require('apollo-server-express')

// 2.定义数据类型
/**
 * query类型是默认客户端查询的类型
 * 该类型在服务端必须存在且唯一
 */

const typeDefs = gql`
  # 输入类型
  input UserInfo {
    uname: String
    pwd: String
  }
  #用户类型
  type User {
    id: ID
    uname: String
    pwd: String
  }
  #变更类型
  type Mutation {
    addUser(userInfo: UserInfo): User
  }
  #查询类型
  type Query {
    hello: String
    msg: String
  }
`

// 3.解析数据类型对应的具体数据
const resolvers = {
  Query: {
    hello: () => 'Hello World!',
    msg: () => 'msg'
  },
  Mutation: {
    addUser: (obj, args) => {
      return {
        id: 123,
        uname: args.userInfo.uname,
        pwd: args.userInfo.pwd
      }
    }
  }
}

// 4.整合apollo-server和express
/**
 *   typeDefs,resolvers 属性名是固定的
 */
const server = new ApolloServer({
  typeDefs,
  resolvers
})

const app = express()
server.applyMiddleware({ app })

// 5.监听启动
app.listen({ port: 4000 }, () => console.log('Now browse to http://localhost:4000' + server.graphqlPath))

/**
 * 客户端查询规则
 * 
query info{
  hello
}

query msg{
  msg
}

mutation addUser{
  addUser(userInfo:{
    uname:"李四"
    pwd:"abs"
  }){
    uname
    pwd
    id
  }
}
 */
